package com.tchintech.pay.widget;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;

import com.tchintech.pay.R;

/**
 * Created by Administrator on 2017/7/25 0025.
 */

public class DialogWiget {
    public String oKText = "是";
    public String cancelText = "否";
    //传值
    public Object obj;

    public interface OnDatePickerOk {
        void onQueryOkClick(View v, DatePicker dp);

        void onQueryOkClick(View v, DatePicker dp, TimePicker tp);
    }


    public interface OnQueryCancel {
        void onQueryCancel(View v);
    }

    public void setOKText(String string) {
        this.oKText = string;

    }

    /**
     * 更新提示有确定和取消2个按钮
     *
     * @param context
     * @param pTitle
     * @param pMsg
     */
    public void showCustomMessageQuery(Context context, String pTitle, final CharSequence pMsg) {
        try {
            final AlertDialog lDialog = new AlertDialog.Builder(context).create();
            //点击空白出不消失
            lDialog.setCanceledOnTouchOutside(false);
            lDialog.show();
            lDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
                @Override
                public void onDismiss(DialogInterface arg0) {
                    arg0.dismiss();
                    if (odml != null) {
                        odml.onDialogMiss(lDialog);
                    }
                }
            });
            Window window = lDialog.getWindow();
            window.setContentView(R.layout.dialog_query);
            ((TextView) window.findViewById(R.id.dialog_title)).setText(pTitle);
            ((TextView) window.findViewById(R.id.dialog_message)).setText(pMsg);
            ((Button) window.findViewById(R.id.bt_ok)).setText(oKText);


            ((Button) window.findViewById(R.id.bt_ok))
                    .setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            lDialog.dismiss();

                            if (oqo != null) {
                                oqo.onQueryOkClick(v);
                            }
                        }
                    });

            ((Button) window.findViewById(R.id.bt_cancel)).setText(cancelText);
            ((Button) window.findViewById(R.id.bt_cancel))
                    .setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            // write your code to do things after users clicks OK
                            lDialog.dismiss();
                            if (oqc != null) {
                                oqc.onQueryCancel(v);
                            }
                        }
                    });
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


    /**
     * 做提示消息
     * 仅有确定按钮一个
     */
    public void showMessage(Context context, String pTitle, final CharSequence pMsg) {
        try {
            final AlertDialog lDialog = new AlertDialog.Builder(context).create();
            //点击空白出不消失
            lDialog.setCanceledOnTouchOutside(false);
            lDialog.show();
            lDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
                @Override
                public void onDismiss(DialogInterface arg0) {
                    arg0.dismiss();
                    if (odml != null) {
                        odml.onDialogMiss(lDialog);
                    }
                }
            });
            Window window = lDialog.getWindow();
            window.setContentView(R.layout.dialog_show);
            ((TextView) window.findViewById(R.id.dialog_title)).setText(pTitle);
            ((TextView) window.findViewById(R.id.dialog_message)).setText(pMsg);
            ((Button) window.findViewById(R.id.bt_ok)).setText("确定");
            ((Button) window.findViewById(R.id.bt_ok))
                    .setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            lDialog.dismiss();
                            if (oqo != null) {
                                oqo.onQueryOkClick(v);
                            }
                        }
                    });

        } catch (Exception e) {
            e.printStackTrace();
        }

    }


    OnDialogMissListener odml;

    public void setOnDialogMissListener(OnDialogMissListener odml) {
        this.odml = odml;
    }

    public interface OnDialogMissListener {
        void onDialogMiss(AlertDialog adlog);
    }

    OnDatePickerOk odpo;
    OnQueryCancel oqc;
    OnQueryOk oqo;

    /**
     * @return the odpo
     */
    public OnDatePickerOk getOnDatePickerOkListener() {
        return odpo;
    }

    public interface OnQueryOk {
        void onQueryOkClick(View v);
    }

    public void setOnQueryOkListener(OnQueryOk onQueryOk) {
        oqo = onQueryOk;
    }

    public void setOnQueryCancelListener(OnQueryCancel onQueryCancel) {
        oqc = onQueryCancel;
    }


}
